//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
//
// --------------------------------------------------------------
//   GEANT 4 - Underground Dark Matter Detector Advanced Example
//
//      For information related to this code contact: Alex Howard
//      e-mail: alexander.howard@cern.ch
// --------------------------------------------------------------
// Comments
//
//            Underground Advanced example main program
//               by A. Howard and H. Araujo 
//                    (27th November 2001)
//
// main program
// --------------------------------------------------------------

#include "G4RunManager.hh"
#include "G4UImanager.hh"

#include "Randomize.hh"

#ifdef G4VIS_USE
#include "G4VisExecutive.hh"
#endif

#ifdef G4UI_USE
#include "G4UIExecutive.hh"
#endif

#include "DMXAnalysisManager.hh"
#include "DMXDetectorConstruction.hh"
#include "DMXPhysicsList.hh"
#include "DMXPrimaryGeneratorAction.hh"
#include "DMXRunAction.hh"
#include "DMXEventAction.hh"
#include "DMXSteppingAction.hh"
#include "DMXStackingAction.hh"
#include "TSystem.h"
#include <vector>

int main(int argc,char** argv) {

  //  DMLROutput* output = new DMLROutput("/scratch/shinichi/OutputFiles/100000_75L_1.root");
  //  DMLROutput* output = new DMLROutput("./75Lpolar_3.root");

  int num=29;

  char treename[128];
  sprintf(treename,"./75L_RPhi_1phase_%d.root",num);
  //  sprintf(treename,"/scratch/shinichi/OutputFiles/75L_RPphi_%d.root",num);
  //char treename[128]="./test.root";

  G4cout<<treename<<G4endl;
  DMLROutput* output = new DMLROutput(treename);

  long seed  = time(NULL);

  // choose the Random engine
  CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine);
  CLHEP::HepRandom::setTheSeed(seed);
  
  // Construct the default run manager
  G4RunManager * runManager = new G4RunManager;

  // set mandatory initialization classes
  DMXDetectorConstruction* detector = new DMXDetectorConstruction;
  runManager->SetUserInitialization(detector);
  runManager->SetUserInitialization(new DMXPhysicsList);

  for(int irun=0;irun<105;irun++){
    // set user action classes
    //    DMXPrimaryGeneratorAction* DMXGenerator = new DMXPrimaryGeneratorAction;
    DMXPrimaryGeneratorAction* DMXGenerator = new DMXPrimaryGeneratorAction(irun+2945);
    runManager->SetUserAction(DMXGenerator);
    DMXRunAction* DMXRun = new DMXRunAction(output);
    runManager->SetUserAction(DMXRun);
    DMXEventAction* eventAction = new DMXEventAction(DMXRun,DMXGenerator);
    runManager->SetUserAction(eventAction);
    runManager->SetUserAction(new DMXSteppingAction(DMXRun,eventAction));
    //  runManager->SetUserAction(new DMXStackingAction);
    
    //Initialize G4 kernel
    runManager->Initialize();
    
    // Define UI session for interactive mode.
    G4UImanager* UImanager = G4UImanager::GetUIpointer();
    G4String command = "/control/execute ";
    G4String fileName = argv[1];
    UImanager->ApplyCommand(command+fileName);
    
  }
  delete output;
  //  delete runManager;
  

  return 0;
}

